package com.pzh.mall.web.controller.quartz;

import cn.hehuoren.core.utils.response.HttpResponseBody;
import cn.hehuoren.quartz.model.QrtzJobRunLog;
import cn.hehuoren.quartz.service.IQrtzJobRunLogService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pzh.sys.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 * @Title 
 * @Description 定时任务日志管理
 * @Copyright Copyright (c) 2015</p>
 * @Company 源本信息科技有限公司 Co., Ltd.</p>
 * @author ZhouMin
 * @version 1.0
 * @修改记录
 * @修改序号，修改日期，修改人，修改内容
 */
@RequestMapping("/api/quartzlog")
@Controller
public class QrtzJobRunLogController extends BaseController {
	public static final Logger logger = LoggerFactory.getLogger(QrtzJobRunLogController.class);
	@Reference
	private IQrtzJobRunLogService qrtzJobRunLogService;
	//@Autowired
	//private String quarztLogFileDir;
	
	@RequestMapping(value = "/goToQryQrtzRunlog.do", method = RequestMethod.GET)
	public String goToQryQrtzRunlog() {
		return "quartz/quartzLogList";
	}
	
	  
    @RequestMapping(value = "/qryQrtzRunlog", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponseBody qryQrtzRunlog(HttpServletRequest request, QrtzJobRunLog qrtzJobRunLog,
												  @RequestParam(required = false, defaultValue = "0") int page,
												  @RequestParam(required = false, defaultValue = "10") int rows, String sidx){
        return HttpResponseBody.successResponse("ok", qrtzJobRunLogService.selectQuartzJobRunLogPage(qrtzJobRunLog, page, rows,null));
    }

    @GetMapping("/getLogDetail")
	@ResponseBody
	public HttpResponseBody getLogDetail(String  id){
		QrtzJobRunLog qrtzJobRunLog = qrtzJobRunLogService.getByKey(id,null,null,null);
		return HttpResponseBody.successResponse("ok", qrtzJobRunLog);
	}
   
	
	/**
	 * 查看日志详情
	 * @param model
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/showQrtzLog", method = RequestMethod.GET)
	public String showQrtzLog(Model model, String  id, HttpServletResponse response) {
		QrtzJobRunLog qrtzJobRunLog = qrtzJobRunLogService.getByKey(id,null,null,null);
		model.addAttribute("qrtzJobRunLog",qrtzJobRunLog);
		//boolean logTag = quartzLogFile(qrtzJobRunLog);
		//model.addAttribute("quartzLogFileTag",logTag==true?"1":"0");
		return "quartz/showQrtzLog";
	}
	
	/**
	 * job执行日志文件下载
	 * @param request
	 * @param response
	 * @param jobName
	 * @param runTime
	 */
	/*
	@RequestMapping(value = "/dowLoadQuartzLogFile.do", method = RequestMethod.GET)
	public void downloadCSVFiles(HttpServletRequest request,	HttpServletResponse response, String jobName, String runTime,String fireId) {
		int bytesRead = 0;
		FileInputStream inputStream = null;
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		try {
			String dateTime =runTime.replaceAll("\\s*", "").replaceAll(":","").replaceAll("-", "");
			if(StringUtils.isBlank(dateTime)||dateTime.length()<8) throw new Exception("格式化日期错误");
			String runDate = dateTime.substring(0,8);
			jobName = jobName+"_"+fireId;
			response.setHeader("Content-Disposition", "attachment;fileName="+jobName+".log");
			if (StringUtils.isBlank(jobName)||StringUtils.isBlank(fireId)||StringUtils.isBlank(runTime))throw new Exception("下载参数不全");
			String path = quarztLogFileDir + "/" + runDate+ "/"+ jobName+".log";
			inputStream = new FileInputStream(new File(path));
			OutputStream os = response.getOutputStream();
			byte[] buffer = new byte[8192];
			while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
		} catch (Exception e) {
			logger.info("", e);
		} finally {
			try {
				if (inputStream != null)
					inputStream.close();
			} catch (Exception e2) {
			}
		}
	}*/
	/**
	 * 判断job执行日志文件是否存在
	 * @param jobName
	 * @param qrtzJobRunLog
	 * @return
	 */
	/*
	private boolean quartzLogFile(QrtzJobRunLog qrtzJobRunLog){
		boolean tag = true;
		DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
		try {
			if(qrtzJobRunLog==null) return false;///throw new Exception("执行日志文件没找到");
			String dateTime = sdf.format(qrtzJobRunLog.getRunStartTime());
			if(StringUtils.isBlank(dateTime)||dateTime.length()<8) throw new Exception("格式化日期错误");
			String runDate = dateTime.substring(0,8);
			String logFilePath = quarztLogFileDir + "/" + runDate+ "/" + qrtzJobRunLog.getJobName()+"_"+qrtzJobRunLog.getFireInstanceId()+".log";
			File logFile =new File(logFilePath);
			if(!logFile.exists()) {
			    logger.info("JOB执行日志文件不存在");
			    return false;
			}
		} catch (Exception e) {
			logger.info("",e);
			tag = false;
		}
		return tag;
	}
	*/
}
